import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
from dbfpy import dbf

'''
read dbf file
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")

    for record in ra:
        #sKey.append('0')
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            sKey.append(int(str(record[item])))

    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey



#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "begin at " + getCurTime()
    filePath = 'C:/_DATA/migration_census_2000/'
    file = filePath + 'continental_cnty_Migrationflows_list.csv'
    
    data = build_data_list(file)

    FIPS_FID_CSV = filePath + 'county_FIPS_FID.csv'
    list = build_data_list(FIPS_FID_CSV)
    fips = {}
    for item in list:
        fips[str(item[0])] = item[1]

    output = []

    for item in data:
        if item[0] <> item[1]:
            temp = [fips[str(item[0])], fips[str(item[1])], item[2]]
            output.append(temp)

    fileLoc = filePath + 'census_county_migration_format.csv'
    np.savetxt(fileLoc, output, delimiter=',', fmt = '%s')

    print "end at " + getCurTime()
    print "========================================================================"  
    
            